package com.gk.carshopsys.controller;

import com.alibaba.excel.EasyExcel;
import com.gk.carshopsys.annotation.Log;
import com.gk.carshopsys.pojo.Car;
import com.gk.carshopsys.pojo.CarOrder;
import com.gk.carshopsys.pojo.CarOrderImport;
import com.gk.carshopsys.pojo.Cust;
import com.gk.carshopsys.service.CarOrderService;
import com.gk.carshopsys.utils.CarDataListener;
import com.gk.carshopsys.utils.CarOrderDataListener;
import com.gk.carshopsys.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CarOrderController {
    @Autowired
    CarOrderService carOrderService;


    //Excel参数
    private HashMap<String,String> param;


    //excel传参
    @PostMapping("/carorderlist/excelparamset")
    @ResponseBody
    public void excelset(@RequestBody HashMap<String,String> params){
        param= params;
    }


    //excel下载
    @Log("导出汽车订单")
    @GetMapping("/carorderlist/excelexport")
    @ResponseBody
    public String excelexport(HttpServletResponse response) throws Exception {
            ExcelUtils.export2Web(response, "汽车订单表", "订单信息",CarOrder.class,carOrderService.findCarOrder(param));
            return "success";
    }

    // easyexcel上传文件
    @Log("导入汽车订单")
    @PostMapping("/carorderlist/excelimport")
    public String upload(MultipartFile file, RedirectAttributesModelMap model) throws IOException {
        EasyExcel.read(file.getInputStream(), CarOrderImport.class, new CarOrderDataListener(carOrderService)).sheet().doRead();
        return "redirect:/carorderlist";
    }

    @RequestMapping("/carorderlist")
    public String toCarOrder(){
        return "carorderlist";
    }

    @Log("查询汽车订单")
    @RequestMapping("/carorderlist/query")
    @ResponseBody
    public List<CarOrder> quryCarOrderList(@RequestBody HashMap<String,String> params){

        return carOrderService.findCarOrder(params);
    }

    @Log("添加汽车订单")
    @RequestMapping("/carorderlist/add")
    public String addCarOrder(@RequestParam("add_carid") Long carid,
                              @RequestParam("add_custid") Long custid,
                              @RequestParam("add_empnum") String empnum,
                              @RequestParam("add_quantity") Integer quantity,
                              @RequestParam("add_state") String state,
                              @RequestParam("add_remarks") String remarks, RedirectAttributesModelMap model){

        int result = carOrderService.addCarOrder(carid,custid,empnum,quantity,state,remarks);

        if (result==-1){
            model.addFlashAttribute("addmsg","添加失败，车辆不存在");
            return "redirect:/carorderlist";
        }
        if (result==-2){
            model.addFlashAttribute("addmsg","添加失败，客户不存在");
            return "redirect:/carorderlist";
        }
        if (result==-3){
            model.addFlashAttribute("addmsg","添加失败，员工不存在");
            return "redirect:/carorderlist";
        }
        if (result==-4){
            model.addFlashAttribute("addmsg","添加失败，库存不足");
            return "redirect:/carorderlist";

        }
        if (result==1){
            model.addFlashAttribute("addmsg","添加订单成功");
            return "redirect:/carorderlist";
        }else{
            model.addFlashAttribute("addmsg","添加订单失败，未知错误");
            return "redirect:/carorderlist";
        }



    }

    @Log("更新汽车订单")
    @RequestMapping("/carorderlist/update")
    public String updateCarOrder(@RequestParam("update_id") Long id,
                                 @RequestParam("update_state") String state,
                                 @RequestParam("update_remarks") String remarks,
                                 @RequestParam("update_quantity") Long quantity,
                                 @RequestParam("update_carid") Long carid,RedirectAttributesModelMap model){
        CarOrder carOrder = new CarOrder();
        carOrder.setId(id);
        carOrder.setRemarks(remarks);
        carOrder.setCarid(carid);
        if(state.equals("取消订单")){
            carOrder.setState("已取消");
        }else{
            carOrder.setState(state);
        }

        int result = carOrderService.updateCarOrderById(carOrder,quantity);

        if (result==1){
            model.addFlashAttribute("addmsg","修改订单成功");
            return "redirect:/carorderlist";
        }else{
            model.addFlashAttribute("addmsg","修改订单失败，未知错误");
            return "redirect:/carorderlist";
        }

    }

    //批量删除订单
    //订单批量删除
    @Log("删除汽车订单")
    @ResponseBody
    @RequestMapping("/carorderlist/del")
    public Map<String,Integer> carorderlistdel(@RequestBody List<Integer> ids){
        int result = carOrderService.delCarOrder(ids);
        Map<String,Integer> map = new HashMap<>();
        map.put("code",result);
        return map;
    }


}
